/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

// eslint-disable-next-line import/no-extraneous-dependencies
import { Request, Response } from 'express';
import { responseSuccess, ResponseItem } from './utils';
import type {
  PddUnTableListItem,
  SearchParams,
  PddComplaintDeatil,
  PddComplaintTypes,
} from '@/pages/CustomerServiceTools/types/PDDComplaintOrder';
import dayjs from 'dayjs';

function getList(req: Request, res: Response, u: string, tableList: Array<any>) {
  let realUrl = u;
  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }
  const { page = 1, pageSize = 10 } = req.body;
  const params = (req.body as unknown) as SearchParams;

  let dataSource = [...tableList].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  // if (params.name) {
  //   dataSource = dataSource.filter((data) => data.name.includes(params.name || ''));
  // }
  const result: ResponseItem = {
    code: 0,
    msg: '',
    data: {
      list: dataSource,
      count: tableList.length,
      page: parseInt(`${params.page}`, 10) || 1,
      pageSize,
    },
  };

  return res.json(result);
}

const generatePddUnFinishList = (page: number, pageSize: number) => {
  const tableList: PddUnTableListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (page - 1) * 10 + i;
    tableList.push({
      id: `${index}`,
      cm_id: `${index}驿站id`,
      league_id: `${index}league_id`,
      waybill_no: `${index}waybill_no`,
      act_name: `${index}操作人`,
      brand: `中国邮政总公司${index}`,
      express_phone: `1357777888${index}`,
      third_user_id: `third_user_id${index}`,
      complaint_source: `complaint_source${index}`,
      complaint_num: `complaint_num${index}`,
      complaint_type: `投诉类型${index}`,
      complaint_type_text: `投诉原因${index}`,
      complaint_desc: `投诉描述${index}`,
      deal_desc: `解决描述${index}`,
      is_deal: `${index % 5 ? 0 : index % 3 ? 1 : index % 2 ? 2 : 3}`,
      create_at: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
      update_at: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
      operate_at: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
      inn_name: `驿站名称${index}`,
      inn_phone: `驿站电话133${index}`,
      last_hour: `${index}`,
      records: {
        act_desc: '补充描述',
        act_info: {
          act_compensate: '0',
          act_desc: '顾客是上帝吖',
          act_img: [
            'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
            'https://img.kuaidihelp.com/wxapp/ws/hy_350.png',
            'https://img.kuaidihelp.com/wxapp/ws/PDD_200.png',
          ],
          act_info: '顾客已取件',
          act_money: '0',
          act_name: '无忌',
          act_source: '客服',
          act_status: '1',
          act_status_text: '完成工单',
          act_tel: '18829900094',
          sign_state: '1',
        },
        complaint_id: '35',
        create_time: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
        id: '6',
      },
    });
  }
  return tableList;
};
const PddTodoList = generatePddUnFinishList(1, 200);

export default {
  // 拼多多工单投诉列表（待处理，二次处理，已完结）
  'POST /PddTicket/getList': (req: Request, res: Response, u: string) =>
    getList(req, res, u, PddTodoList),
  // 平多多工单投诉详情
  'POST /PddTicket/getComplaintInfo': (req: Request, res: Response, u: string) => {
    const { id, status } = req.body;
    const data: PddComplaintDeatil = {
      complaint: {
        id: `${id}`,
        cm_id: `${id}驿站id`,
        league_id: `${id}league_id`,
        waybill_no: `${id}waybill_no`,
        act_name: `${id}操作人`,
        brand: `中国邮政总公司${id}`,
        express_phone: `1357777888${id}`,
        third_user_id: `third_user_id${id}`,
        complaint_source: `complaint_source${id}`,
        complaint_num: `complaint_num${id}`,
        complaint_type: `投诉类型${id}`,
        complaint_type_text: `投诉原因${id}`,
        complaint_desc: `投诉描述${id}`,
        deal_desc: `解决描述${id}`,
        is_deal: status,
        create_at: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
        update_at: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
        operate_at: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
        inn_name: `驿站名称${id}`,
        inn_phone: `驿站电话133${id}`,
        last_hour: `${id}`,
      },
      pdd_gd: {
        title: `${id}找不到快递`, // 投诉原因
        receive_name: '客户名称', // 客户名称
        receive_address: '客户地址',
        receive_contact: '客户联系方式',
        ticket_id: `${id}工单号`, // 工单号
        type_text: `${id}找不到包裹`, // 投诉原因
      },
      inn: {
        cm_id: `${id}被投诉驿站ID`, // 驿站id
        inn_name: `${id}被投诉驿站名称`, // 驿站名称
        phone: `${id}驿站号码`, // 驿站号码
      },
      record: [
        {
          act_desc: '补充描述',
          act_info: {
            act_compensate: `${id % 3 ? 0 : id % 2 ? 1 : 2}`, // 是否需要赔偿 0：默认，1：未赔偿，2：已赔付
            act_desc: '处理结果详细', // 处理结果详细
            act_img: [
              'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
              'https://img.kuaidihelp.com/wxapp/ws/hy_350.png',
              'https://img.kuaidihelp.com/wxapp/ws/PDD_200.png',
            ],
            act_info: '已送货上门',
            act_money: '3',
            act_name: '驿站操作人',
            act_source: '驿站',
            act_status: '1',
            act_status_text: '完成工单',
            act_tel: '13566667777',
            sign_state: `${id % 3 ? 0 : id % 2 ? 1 : 2}`, // 签收状态 0：默认，1：未签收，2：已签收
          },
          complaint_id: id,
          create_time: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
          id: '6',
        },
        {
          act_desc: '补充描述2',
          act_info: {
            act_compensate: `${id % 3 ? 0 : id % 2 ? 1 : 2}`, // 是否需要赔偿 0：默认，1：未赔偿，2：已赔付
            act_desc: '处理结果详细2', // 处理结果详细
            act_img: [
              'https://img.kuaidihelp.com/wxapp/ws/hy_350.png',
              'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
              'https://img.kuaidihelp.com/wxapp/ws/PDD_200.png',
            ],
            act_info: '包裹丢失',
            act_money: '4',
            act_name: '',
            act_source: '客服',
            act_status: '3',
            act_status_text: '已送货上门',
            act_tel: '13566667777',
            sign_state: `${id % 3 ? 0 : id % 2 ? 1 : 2}`, // 签收状态 0：默认，1：未签收，2：已签收
          },
          complaint_id: id,
          create_time: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
          id: '7',
        },
      ],
    };
    const result: ResponseItem = {
      code: 0,
      msg: '',
      data,
    };
    return res.json(result);
  },
  'POST /PddTicket/passRecord': responseSuccess,
  'POST /PddTicket/getComplaintTypeInfo': (req: Request, res: Response, u: string) => {
    let data: PddComplaintTypes = {
      '1': '处理中',
      '2': '已电话致歉，顾客谅解',
      '3': '包裹丢失',
    };
    const result: ResponseItem = {
      code: 0,
      msg: '',
      data,
    };
    return res.json(result);
  },
  // 拼多多投诉提交
  'POST /PddTicket/ticketSave': (req: Request, res: Response) => responseSuccess(req, res),
};
